﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using emp.Utility;
using System.Data.SqlClient;
using System.Data;
using emp.Model.SysManage;

public partial class crm_memberApprove_MemberApproveBatch : System.Web.UI.Page
{
    private DataTable getTableInfo(string sql)
    {
        DataSet data = new DataSet();
        using (SqlDataAdapter sda = new SqlDataAdapter())
        {
            try
            {
                sda.SelectCommand = new SqlCommand();
                sda.SelectCommand.CommandText = sql;
                sda.SelectCommand.Connection = new SqlConnection(emp.SqlServerDAL.SQLHelper.CONN_STRING_NON_DTC);
                sda.Fill(data);
            }
            catch { }
        }
        return data.Tables[0];
    }


    private bool ExecSql(string sql)
    {
        bool ret = false;
        using (SqlConnection conn = new SqlConnection(emp.SqlServerDAL.SQLHelper.CONN_STRING_NON_DTC))
        {
            conn.Open();
            using (SqlTransaction trans = conn.BeginTransaction())
            {
                try
                {
                    emp.SqlServerDAL.SQLHelper.ExecuteNonQuery(trans, CommandType.Text, sql, null);
                    trans.Commit();
                    ret = true;
                }
                catch (SqlException err)
                {
                    trans.Rollback();
                }
            }
        }
        return ret;
    }

    private IList getDt() 
    {
        IList list = new ArrayList();
        string sql = "SELECT  IRB_UID      ,IRB_XLS_FILE      ,IRB_STATUS      ,IRB_UPLOAD_NUM      ,IRB_UPLOAD_USER      ,IRB_UOLOAD_DATE      ,IRB_REMARK ,(select [user_name] from [SYSTEM_USER_TBL] where [user_uid]=IRB_UPLOAD_USER) as IRB_DOLOAD_USER_NAME   FROM  INDIVIDUAL_RESPONSE_BATCH   WHERE 1=1 ";
        if (DATE_BEGN.Text != null && DATE_BEGN.Text != "")
        {
            sql += " and IRB_UOLOAD_DATE>='" + DATE_BEGN.Text + "  00:00:00'";
        }
        if (DATE_END.Text != null && DATE_END.Text != "")
        {
            sql += " and IRB_UOLOAD_DATE<='" + DATE_END.Text + "  23:59:59'";
        }
        if (OUB_STATUS.SelectedIndex != 0)
        {
            sql += " and IRB_STATUS='" + OUB_STATUS.SelectedValue + "'";
        }


        UserInfo myUserInfo = new UserInfo();
        if ((UserInfo)Session["USER_KEY"] != null)
        {
            myUserInfo = (UserInfo)Session["USER_KEY"];

        }
        string uid = myUserInfo.UserID.ToString();
        string uids = System.Configuration.ConfigurationSettings.AppSettings["publicRole"];
        string[] op = uids.Split(',');
        for (int i = 0; i < op.Length; i++)
        {

            string sql11 = "SELECT * from [SYSTEM_ROLE_USER] where [role_uid]='" + op[i] + "' and [user_uid]='" + myUserInfo.UserID.ToString() + "'";
            System.Data.DataTable dt111 = getTableInfo(sql11);

            if (dt111.Rows.Count > 0)
            {
                uid = "";
            }
        }

        System.Data.DataTable dt1111 = getTableInfo("select * from [SYSTEM_USER_TBL] where [user_uid]=" + myUserInfo.UserID.ToString());
        if (dt1111.Rows.Count > 0 && dt1111.Rows[0]["user_type"].ToString() == "MANAGER")
        {
            sql += " and IRB_UPLOAD_USER in (select [user_uid] from SYSTEM_USER_TBL where [user_region_id]='" + myUserInfo.UserID.ToString() + "' ) ";
        }
        else
        {
            if (uid != null && uid != "")
            {
                sql += " and IRB_UPLOAD_USER = '" + uid + "' ";
            }
        }




        sql += " order by  IRB_UID desc";
        System.Data.DataTable dt1 = getTableInfo(sql);
        for (int i = 0; i < dt1.Rows.Count; i++)
        {
            OgilvyOne.Model.INDIVIDUAL_RESPONSE_BATCH irb = new OgilvyOne.Model.INDIVIDUAL_RESPONSE_BATCH();
            irb.IRB_ID = int.Parse(dt1.Rows[i]["IRB_UID"].ToString());

            irb.IRB_XLS_FILE = dt1.Rows[i]["IRB_XLS_FILE"].ToString();
            irb.IRB_STATUS = dt1.Rows[i]["IRB_STATUS"].ToString();
            irb.IRB_VALID_NUM = int.Parse(dt1.Rows[i]["IRB_UPLOAD_NUM"].ToString());
            irb.IRB_DOLOAD_USER_NAME = dt1.Rows[i]["IRB_DOLOAD_USER_NAME"].ToString();
            irb.IRB_DOLOAD_DATE =( DateTime.Parse(dt1.Rows[i]["IRB_UOLOAD_DATE"].ToString())).ToShortDateString();

            


            irb.IRB_REMARK = dt1.Rows[i]["IRB_REMARK"].ToString();
            list.Add(irb);
        }

        return list;
    }

    private void getGV() 
    {



        IList list = getDt();

        this.campaignList.DataSource = list;
        this.campaignList.DataBind();


        PageUtil.setPageCount(campaignList, list);

        if (list.Count == 0)
        {
            LBjl.Visible = true;
        }
        else
        {
            LBjl.Visible = false;
        }



    }



    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            getGV();

        }
    }


    protected void PageChanged(object sender, GridViewPageEventArgs e)
    {

        gview(e.NewPageIndex);

    }

    private void gview(int page)
    {


        campaignList.PageIndex = PageUtil.getGviewNewPage(campaignList, page);
        this.pageNum.Value = campaignList.PageIndex.ToString();

        IList clist = getDt();

        this.campaignList.DataSource = clist;
        this.campaignList.DataBind();

        PageUtil.setPageCount(campaignList, clist);

    }


    protected void upLoad0_Click1(object sender, EventArgs e)
    {
        Response.Redirect("EducePage.aspx", true);
    }
    protected void upLoad_Click(object sender, EventArgs e)
    {

        getGV();

    }
}
